package hittegods.gui;

import hittegods.classes.*;
import java.awt.Dimension;
import java.awt.event.*;
import java.sql.Date;
import javax.swing.*;

public class checkoutElementPnl extends JPanel{
    private Item item;
    private JLabel caseNoLbl;
    private JLabel itemDescLbl;
    private JLabel rewardAmountLbl;
    private JTextField rewardAmount;
    private JButton checkoutBtn;
    private JLabel reqRewValLbl;
    private JLabel finderLbl;
    private boolean needsReward;
    private Reward reward;
    private Mainscreennew frame;

    public checkoutElementPnl(Mainscreennew frame,Item item) {
        this.item = item;
        this.frame = frame;
        setBorder(javax.swing.BorderFactory.createTitledBorder(null, "Afskriv effekt", javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font("Arial", 0, 18)));
        setBounds(0, 0, WIDTH - 25, HEIGHT - 25);
//        init();
//        drawPanel();
    }

    private void drawPanel(){
        this.setLayout(null);

        Dimension dimension = new Dimension();

        add(caseNoLbl);
        Dimension size = caseNoLbl.getPreferredSize();
        caseNoLbl.setBounds(20, 25, size.width, 23);

        add(itemDescLbl);
        size = itemDescLbl.getPreferredSize();
        itemDescLbl.setBounds(20, 45, size.width, 23);

        add(finderLbl);
        size = finderLbl.getPreferredSize();
        finderLbl.setBounds(20, 65, size.width, 23);

        add(reqRewValLbl);
        size = reqRewValLbl.getPreferredSize();
        reqRewValLbl.setBounds(20, 85, size.width, 23);

        add(rewardAmountLbl);
        size = rewardAmountLbl.getPreferredSize();
        rewardAmountLbl.setBounds(20, 125, size.width, 23);

        add(rewardAmount);
        size = rewardAmount.getPreferredSize();
        rewardAmount.setBounds(20, 145, 100, 23);

        add(checkoutBtn);
        size = checkoutBtn.getPreferredSize();
        checkoutBtn.setBounds(20, 165, 100, 23);
        setVisible(true);

        checkoutBtn.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                 checkoutElement();
            }
        });

    }

    private void init(){
        caseNoLbl = new JLabel("Sag nummer: " + item.getCaseNo());
        itemDescLbl = new JLabel(item.getDescription());
        rewardAmountLbl = new JLabel("Indtast findeløn hvis den gives:");
        reqRewValLbl = new JLabel();
        finderLbl = new JLabel("Fundet af: " + item.getFinderName());


        reward = new Reward(item.getCaseNo());

        if(reward.getReqValue() == 0.0 || reward == null){
            reqRewValLbl.setText("Finder har ikke ønsket nogen findeløn");
            needsReward = false;
        }else{
            reqRewValLbl.setText("Finder har ønsket KR" + reward.getReqValue() + " i findeløn");
            rewardAmountLbl.setText("Indtast den givne findeløn");
            needsReward = true;
        }

        rewardAmount = new JTextField();
        rewardAmount.setText("0");

        checkoutBtn = new JButton("Udlever");


    }

    public void checkoutElement(){
        if(needsReward && rewardAmount.getText().equalsIgnoreCase("0")){
            JOptionPane.showMessageDialog(this, "Finderen har ønsket en findeløn");
        }else{
            Date date = new Date(System.nanoTime());
            int itemStatus = item.editItem(item.getCategoryId(), item.getDescription(), item.getFoundDate(), item.getFoundPlace(), item.getFinderName(), item.getOwnerName(), date, true, 2);
            int rewardStatus = reward.updateReward(rewardAmount.getText());
            System.out.println("item " + itemStatus);
            System.out.println("reward " + rewardStatus);
            if((itemStatus==1) && (rewardStatus==1)){
                JOptionPane.showMessageDialog(this, "Effekten er checket ud af systemet");
            }else{
                JOptionPane.showMessageDialog(this, "En fejl opstod og effekten kunne ikke checkes ud!");
            }
        }
    }
}